﻿#include "form_log.h"
#include "ui_form_log.h"

#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QDir>
#include <QCompleter>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif




struct LOG
{
	QString op_id;
	QString device;
	QString dateTime;
	QString cause;
	QString level;
	QString deal;
	QString op_time;
	QString op;
	LOG() {}
	LOG(QString v1, QString v2, QString v3, QString v4, QString v5, QString v6, QString v7, QString v8)
	{
		op_id = v1;
		device = v2;
		dateTime = v3;
		cause = v4;
		level = v5;
		deal = v6;
		op_time = v7;
		op = v8;
	}

	QJsonObject toJson()
	{
		QJsonObject jObj;
		jObj.insert("op_id", op_id);
		jObj.insert("device", device);
		jObj.insert("dateTime", dateTime);
		jObj.insert("cause", cause);
		jObj.insert("level", level);
		jObj.insert("deal", deal);
		jObj.insert("op_time", op_time);
		jObj.insert("op", op);

		return jObj;
	}

	void fromJson(QJsonObject obj)
	{

		op_id = obj.value("op_id").toString();
		device = obj.value("device").toString();
		dateTime = obj.value("dateTime").toString();
		cause = obj.value("cause").toString();
		level = obj.value("level").toString();
		deal = obj.value("deal").toString();
		op_time = obj.value("op_time").toString();
		op = obj.value("op").toString();

	}
};

QList< LOG*> log_info
{
new LOG("管理员","手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "一级", "未处理","2023/5/30 10:54:35","删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级","未处理", "2023/5/30 10:54:35","删除"),
new LOG("管理员","手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "一级", "未处理","2023/5/30 10:54:35","删除"),
new LOG("管理员","手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备",  "一级","未处理","2023/5/30 10:54:35","删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级","未处理","2023/5/30 10:54:35", "删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温","一级",  "未处理","2023/5/30 10:54:35","删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级","未处理", "2023/5/30 10:54:35","删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级", "未处理","2023/5/30 10:54:35","删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级", "未处理","2023/5/30 10:54:35","删除"),
new LOG("管理员","手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "一级","未处理", "2023/5/30 10:54:35","删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级","未处理","2023/5/30 10:54:35", "删除"),
new LOG("管理员","核反应堆",   "2023/5/30 10:54:35", "温度过高，请注意降温", "一级","未处理", "2023/5/30 10:54:35","删除"),
new LOG("管理员","手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "一级","未处理","2023/5/30 10:54:35", "删除"),
new LOG("管理员","手动截止阀", "2023/5/30 10:54:35", "一号环路冷却剂流量低，需更换设备", "一级", "未处理", "2023/5/30 10:54:35","删除"),
};


void FormLog::saveJson()
{
	QDir dir(qApp->applicationDirPath());
	if (!dir.exists("data"))
		dir.mkpath("data");

	QJsonArray headerArray;
	QStringList header_label;
	header_label << "序号" << "操作ID" << "故障设备" << "故障时间" << "故障原因" << "故障等级" << "是否处理" << "操作时间" << "操作";
	//headerArray.fromStringList(header_label);
	foreach(QString str, header_label)
	{
		headerArray.append(str);

	}


	QJsonArray dataArray;
	foreach(LOG * json, log_info) {
		dataArray.append(json->toJson());
	}


	QJsonObject content;
	content["header"] = headerArray;
	content["data"] = dataArray;


	QByteArray bytes = QJsonDocument(content).toJson();

	QFile fileArray(QApplication::applicationDirPath() + "/data/jsonLog.json");
	if (fileArray.open(QIODevice::WriteOnly))
	{
		fileArray.write(bytes);
		fileArray.close();
	}
}

bool FormLog::readJson()
{

	QFile fileArray(QApplication::applicationDirPath() + "/data/jsonLog.json");
	if (fileArray.open(QIODevice::ReadOnly))
	{
		log_info.clear();
		QByteArray bytes = fileArray.readAll();
		QJsonDocument doc = QJsonDocument::fromJson(bytes);

		QJsonObject array = doc.object();
		QJsonArray headerArray = array.value("header").toArray();
		QJsonArray dataArray = array.value("data").toArray();

		int cnt = dataArray.count();
		for (int i = 0; i < cnt; i++)
		{
			QJsonObject  obj = dataArray.at(i).toObject();
			LOG* log = new LOG;
			log->fromJson(obj);
			log_info.append(log);
		}

		return true;
	}
	return false;
}

FormLog::FormLog(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::FormLog)
{
	ui->setupUi(this);

	if (!readJson())
		saveJson();

	init_tableview();

	ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

	ui->tableView->setColumnWidth(0, 80);
	ui->tableView->setColumnWidth(1, 180);
	ui->tableView->setColumnWidth(2, 180);
	ui->tableView->setColumnWidth(3, 180);
	ui->tableView->setColumnWidth(4, 280);
	ui->tableView->setColumnWidth(5, 180);

	QSet<QString>device_list;
	QSet<QString>reson_list;
	for (int i = 0; i < log_info.count(); i++)
	{
		device_list.insert(log_info[i]->device);
		reson_list.insert(log_info[i]->cause);
	}

	//QCompleter* completer = new QCompleter(device_list, this);
	//completer->setCaseSensitivity(Qt::CaseInsensitive);
	//completer->setCompletionMode(QCompleter::InlineCompletion);

	ui->comboBox_device->addItems(device_list.toList());//> setCompleter(completer);

	//QCompleter* completer_2 = new QCompleter(reson_list, this);
	//completer_2->setCaseSensitivity(Qt::CaseInsensitive);
	//completer->setCompletionMode(QCompleter::InlineCompletion);

	ui->comboBox_reson->addItems(reson_list.toList());

}

FormLog::~FormLog()
{
	delete ui;
}

void FormLog::init_tableview()
{
	QStringList header_label;
	header_label << "序号" << "操作ID" << "故障设备" << "故障时间" << "故障原因" << "故障等级" << "是否处理" << "操作时间";// << "操作";

	sortModel = new SortLogModel;
	model_data = new QStandardItemModel;
	model_data->setHorizontalHeaderLabels(header_label);
	model_data->setRowCount(log_info.count());


	update_tableview();

	int row = 0;
	foreach(LOG * log, log_info)
	{

		QModelIndex index_6 = model_data->index(row, 8);

		QPushButton* btn = new QPushButton("删除");
		ui->tableView->setIndexWidget(index_6, btn);


		connect(btn, &QPushButton::clicked, [=] {

			disconnect(btn, &QPushButton::clicked, nullptr, nullptr);

			model_data->removeRow(row);

			log_info.removeOne(log);

			saveJson();

			ui->tableView->reset();

			init_tableview();
			//	

			//update_tableview();
			sortModel->setSourceModel(model_data);
			ui->tableView->setModel(sortModel);
			});

		row++;
	}


	sortModel->setSourceModel(model_data);
	ui->tableView->setModel(sortModel);

	ui->tableView->resizeColumnsToContents();
}


void  FormLog::update_tableview()
{
	int row = 0;
	foreach(LOG * log, log_info)
	{
		QModelIndex index_0 = model_data->index(row, 0);
		model_data->setData(index_0, row + 1);

		QModelIndex index_1 = model_data->index(row, 1);
		model_data->setData(index_1, log->op_id);

		QModelIndex index_2 = model_data->index(row, 2);
		model_data->setData(index_2, log->device);

		QModelIndex index_3 = model_data->index(row, 3);
		model_data->setData(index_3, log->dateTime);

		QModelIndex index_4 = model_data->index(row, 4);
		model_data->setData(index_4, log->cause);

		QModelIndex index_5 = model_data->index(row, 5);
		model_data->setData(index_5, log->level);

		QModelIndex index_6 = model_data->index(row, 6);
		model_data->setData(index_6, log->deal);

		QModelIndex index_7 = model_data->index(row, 7);
		model_data->setData(index_7, log->op_time);

		//QModelIndex index_8 = model_data->index(row, 8);
		//model_data->setData(index_8, log->op);

		row++;
	}
}

void FormLog::on_lineEdit_device_editingFinished()
{
	//	QString str_search = ui->lineEdit_device->text();

		//   ui->tableView->search(str_search);


}


void FormLog::on_lineEdit_reson_editingFinished()
{
	//	QString str_search = ui->lineEdit_reson->text();

		// ui->tableView->search(str_search);


}




void FormLog::on_comboBox_reson_textActivated(const QString& arg1)
{
	QString str_search = arg1;// ui->comboBox_device->currentText();



	//QRegExp regExp(str_search, Qt::CaseSensitive, QRegExp::RegExp);
	//通过^(-?\d+)(\.\d+)?$来匹配整数
	//sortModel->setFilterRegExp(regExp);   //安装过滤器

	sortModel->filter = str_search;
	sortModel->col = 4;

	sortModel->setFilterKeyColumn(4);
}


void FormLog::on_comboBox_device_textActivated(const QString& arg1)
{
	QString str_search = arg1;// ui->comboBox_device->currentText();



	QRegExp regExp(str_search, Qt::CaseSensitive, QRegExp::RegExp);
	//通过^(-?\d+)(\.\d+)?$来匹配整数
	sortModel->setFilterRegExp(regExp);   //安装过滤器

	sortModel->filter = str_search;
	sortModel->col = 2;

	sortModel->setFilterKeyColumn(2);
}


void FormLog::on_pushButton_filter_clicked()
{

	sortModel->date_1 = ui->dateEdit->dateTime();
	sortModel->date_2 = ui->dateEdit_2->dateTime();

	sortModel->col = 3;

	sortModel->setFilterKeyColumn(3);

}

